# Maintainer: Philippe Renon <philippe.renon@yahoo.fr>

_realname=vrpn
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.34
_pkgver=0${pkgver}
pkgrel=6
pkgdesc="The Virtual-Reality Peripheral Network (mingw-w64)"
arch=('any')
url="https://github.com/vrpn/vrpn/wiki"
license=("BSL1.0")
depends=("${MINGW_PACKAGE_PREFIX}-hidapi"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-swig")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/${_realname}/${_realname}/archive/v${_pkgver}.tar.gz"
        "0001-msys2-fix-jsoncpp-related-link-failure.patch"
        "0002-fix-shared-build.patch")
sha256sums=('ad2e26ad7493c2b9708eacf63e575d25a6e582f1d5d63d8cc361d4e79a8ced8d'
            '003d378b18bfd8a38cf3562e56db6c081b3a1547b7bb7c53504ebb96b0bb9b64'
            '21244cdc98bd23b6faf5ada5219b848bc79efe30d019af5a63e37bcb203ff687')

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"
  
  patch -p1 -i ${srcdir}/0001-msys2-fix-jsoncpp-related-link-failure.patch
  patch -p1 -i ${srcdir}/0002-fix-shared-build.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DDIRECTX_ROOT_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
    -DWINHID_ROOT_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
    -DVRPN_USE_SHARED_LIBRARY=ON \
    -DVRPN_USE_WINSOCK2=ON \
    "${extra_config[@]}" \
    ../${_realname}-${_pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  install -Dm644 ${srcdir}/${_realname}-${_pkgver}/README.Legal "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
